using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenComposite.EII.Repository;
using System.Collections;
using System.Diagnostics;
using OpenComposite.Base.Collections;

namespace OpenComposite.EII.Forms
{
	public partial class FormSelectRepositoryEx : Form
	{
		#region Initialization
		protected FormSelectRepositoryEx(
			IServiceProvider serviceProvider,
			int inputBObjectID, int outputBObjectID,
			int workflowID,
			bool useFilterByIOBObjects, bool showWorkflowSpecific)
		{
			_serviceProvider = serviceProvider;
			_idInputBObject = inputBObjectID;
			_idOutputBObject = outputBObjectID;
			_bFilterByIOBObjects = useFilterByIOBObjects;
			_idWorkflow = workflowID;
			_bShowWorkflowSpecific = showWorkflowSpecific;

			imageList = Helpers.GetRepositoryItemTypeImageList();
		}
		public FormSelectRepositoryEx(IServiceProvider serviceProvider)
			: this(serviceProvider, -1, -1, -1, false, false)
		{
			InitializeComponent();

			init();
		}
		public FormSelectRepositoryEx(IServiceProvider serviceProvider,
			int inputBObjectID, int outputBObjectID)
			: this(serviceProvider, inputBObjectID, outputBObjectID, -1, true, false)
		{
			InitializeComponent();

			init();
		}

		public FormSelectRepositoryEx(IServiceProvider serviceProvider, int workflowID)
			: this(serviceProvider, -1, -1, workflowID, false, ( workflowID > 0 ))
		{
			InitializeComponent();

			init();
		}

		private void init()
		{
			_dtHierarchy = null;
			_dtMain = null;
			_lastCheckedCell = null;

			_bMultiSelect = true;
			_bShowAllDomains = true;
			_bShowAllFolders = true;
			_bSearchMode = false;

			_idRepository = Global.Status.CurrentRepositoryId;
			_idDomain = Global.Status.CurrentDomainId;
			_idParent = -1;
			_typeParent = ItemType.Undefined;

			_parentItem = null;

			_allowedTypes = new List<ItemType>();
			_addionalItems = new List<IRepositoryItem>();
			_selectedItems = new List<RepositoryItemKey>();

			tscmbDomain.ComboBox.DrawMode = DrawMode.OwnerDrawFixed;
			tscmbDomain.ComboBox.DrawItem += new DrawItemEventHandler(ComboBox_DrawItem);
			tscmbFolder.ComboBox.DrawMode = DrawMode.OwnerDrawFixed;
			tscmbFolder.ComboBox.DrawItem += new DrawItemEventHandler(ComboBox_DrawItem);

			dgvItems.AutoGenerateColumns = false;

			tscmbSearchColumn.Items.Clear();
			tscmbSearchColumn.Items.AddRange(_fields.GetItems());
			tscmbSearchColumn.SelectedIndex = 0;

			this.ShowParentItemOnly = false;
		}
		#endregion

		#region Public Members

		#region Properties

		public ItemType[] AllowedTypes
		{
			get { return _allowedTypes.ToArray(); }
			set
			{
				if ( _allowedTypes == null )
					_allowedTypes = new List<ItemType>();
				else
					_allowedTypes.Clear();

				_allowedTypes.AddRange(value);
				OnAllowedTypesChanged();
			}
		}
		public IRepositoryItem[] AdditionalItems
		{
			get { return _addionalItems.ToArray(); }
			set
			{
				_addionalItems.Clear();
				_addionalItems.AddRange(value);
				OnAdditionalItemsChanged();
			}
		}
		public bool AllowChangeType
		{
			get { return tsbtnCurrentType.Visible; }
			set { tsbtnCurrentType.Visible = value; }
		}
		public bool AllowChangeDomain
		{
			get { return tscmbDomain.Visible; }
			set
			{
				tslblDomain.Visible = value;
				tscmbDomain.Visible = value;
			}
		}
		public bool AllowCreateNewItem
		{
			get { return tsbtnCreateNew.Visible; }
			set { tsbtnCreateNew.Visible = value; }
		}

		[DefaultValue(true)]
		public bool MultiSelect
		{
			get { return _bMultiSelect; }
			set { _bMultiSelect = value; }
		}

		[DefaultValue(true)]
		public bool ShowAllDomains
		{
			get { return _bShowAllDomains; }
			set { _bShowAllDomains = value; }
		}
		[DefaultValue(true)]
		public bool ShowAllFolders
		{
			get { return _bShowAllFolders; }
			set { _bShowAllFolders = value; }
		}
		[DefaultValue(false)]
		public bool ShowParentItemOnly
		{
			get;
			set;
		}

		public int RepositoryID
		{
			get { return _idRepository; }
			set { _idRepository = value; }
		}
		public int DomainID
		{
			get { return _idDomain; }
			set { _idDomain = value; }
		}
		public int FolderID
		{
			get { return _idFolder; }
			set { _idFolder = value; }
		}
		public int ParentID
		{
			get { return _idParent; }
			set { _idParent = value; }
		}
		public ItemType ParentType
		{
			get { return _typeParent; }
			set { _typeParent = value; }
		}

		public IRepositoryItem ParentItem
		{
			get { return _parentItem; }
			set { _parentItem = value; }
		}

		public IRepositoryItem SelectedItem
		{
			get
			{
				Debug.Assert(!this.MultiSelect, "Do not use get_SingleSelectedItem if MultiSelect is true. Use GetSelectedItems method in this case.");
				if ( _selectedItems.Count > 0 ) {
					return this.RepositoryService.GetItem(_selectedItems[0].Type, _selectedItems[0].ID, false);
				} else {
					return null;
				}
			}
			set
			{
				Debug.Assert(!this.MultiSelect, "Do not use set_SingleSelectedItem if MultiSelect is true. Use SetSelectedItems method in this case.");
				_selectedItems.Clear();
				if ( value != null )
					_selectedItems.Add(new RepositoryItemKey(value));
			}
		}

		#endregion

		#region Methods

		public void SetSelectedItems(IEnumerable<IRepositoryItem> lstSelected)
		{
			if ( lstSelected == null ) return;
			foreach ( IRepositoryItem item in lstSelected ) {
				_selectedItems.Add(new RepositoryItemKey(item));
			}
		}
		public List<IRepositoryItem> GetSelectedItems()
		{
			RepositoryService repSvc = this.RepositoryService;
			if ( repSvc == null )
				throw new NullReferenceException("Repository Service not available.");

			List<IRepositoryItem> lstSelected = new List<IRepositoryItem>();

			foreach ( RepositoryItemKey key in _selectedItems ) {
				IRepositoryItem item = repSvc.GetItem(key.Type, key.ID, false);
				if ( item != null && !lstSelected.Contains(item) )
					lstSelected.Add(item);
			}

			return lstSelected;
		}

		#endregion

		#endregion

		#region Protected Members

		#region Properties

		protected Domain SelectedDomain
		{
			get
			{
				DomainItem di = tscmbDomain.SelectedItem as DomainItem;
				if ( di != null ) {
					return di.Item as Domain;
				} else {
					return null;
				}
			}
		}
		protected IRepositoryItem SelectedFolderItem
		{
			get
			{
				FolderItem fi = tscmbFolder.SelectedItem as FolderItem;
				if ( fi != null ) {
					return fi.Item;
				} else {
					return null;
				}
			}
		}
		protected RepositoryService RepositoryService
		{
			get { return (RepositoryService)this.GetService(typeof(RepositoryService)); }
		}

		#endregion

		#region Methods

		// new 
		protected virtual void OnAllowedTypesChanged()
		{
			showAllowedTypes();
		}
		protected virtual void OnAdditionalItemsChanged()
		{
			fillMainList();
		}

		// override
		protected override object GetService(Type service)
		{
			if ( _serviceProvider != null ) {
				return _serviceProvider.GetService(service);
			} else {
				object svc = Global.Data.RepositoryService.GetService(service);
				if ( svc == null ) {
					svc = base.GetService(service);
				}
				return svc;
			}
		}
		#endregion

		#endregion

		#region Private Members

		#region Variables

		private bool _bMultiSelect;
		private bool _bShowAllDomains;
		private bool _bShowAllFolders;
		private bool _bFilterByIOBObjects;
		private bool _bSearchMode;
		private bool _bShowWorkflowSpecific;

		private int _idRepository;
		private int _idDomain;
		private int _idFolder;
		private int _idParent;
		private ItemType _typeParent;

		private int _idInputBObject;
		private int _idOutputBObject;

		private int _idWorkflow;

		private IRepositoryItem _parentItem;

		private List<ItemType> _allowedTypes;
		private List<IRepositoryItem> _addionalItems;

		private List<RepositoryItemKey> _selectedItems;
		private DataGridViewCheckBoxCell _lastCheckedCell;

		private DataTable _dtMain;
		private DataTable _dtHierarchy;
		private BindingListEx<RepositoryItemBase> _listMain;

		private IServiceProvider _serviceProvider;

		private ImageList imageList;

		#endregion

		#region Constants
		private const int _textGap = 18;
		private const int _indentWidth = 10;
		#endregion

		#region Methods

		private void showAllowedTypes()
		{
			domainsToolStripMenuItem.Visible = _allowedTypes.Contains(ItemType.Domain);
			sOAsToolStripMenuItem.Visible = _allowedTypes.Contains(ItemType.SOA);
			businessProcessesToolStripMenuItem.Visible = _allowedTypes.Contains(ItemType.Business_Process);
			sOBAsToolStripMenuItem.Visible = _allowedTypes.Contains(ItemType.SOBA);
			vocabularyToolStripMenuItem.Visible = _allowedTypes.Contains(ItemType.Noun);
			capabilitiesToolStripMenuItem.Visible = _allowedTypes.Contains(ItemType.Capability);
			capabilityMethodsToolStripMenuItem.Visible = _allowedTypes.Contains(ItemType.Verb);
			businessObjectsToolStripMenuItem.Visible = _allowedTypes.Contains(ItemType.BObject);
			businessRulesToolStripMenuItem.Visible = _allowedTypes.Contains(ItemType.BRule);
			compositesToolStripMenuItem.Visible = _allowedTypes.Contains(ItemType.Composite);
			componentsToolStripMenuItem.Visible = _allowedTypes.Contains(ItemType.Component);
			orchestrationsToolStripMenuItem.Visible = _allowedTypes.Contains(ItemType.Workflow);
			webServicesToolStripMenuItem.Visible = _allowedTypes.Contains(ItemType.WebService);
			ruleConstantsToolStripMenuItem.Visible = _allowedTypes.Contains(ItemType.RuleConstant);
			humanActivitiesToolStripMenuItem.Visible = _allowedTypes.Contains(ItemType.HumanActivity);
			capabilitiesToolStripMenuItem.Visible = _allowedTypes.Contains(ItemType.Capability);
			logicalMethodGroupsToolStripMenuItem.Visible = _allowedTypes.Contains(ItemType.LogicalMethodGroup);
			logicalMethodsToolStripMenuItem.Visible = _allowedTypes.Contains(ItemType.LogicalWebMethod);

			tsmiNewBObject.Visible = _allowedTypes.Contains(ItemType.BObject);
			tsmiNewBProcess.Visible = _allowedTypes.Contains(ItemType.Business_Process);
			tsmiNewBRule.Visible = _allowedTypes.Contains(ItemType.BRule);
			tsmiNewService.Visible = _allowedTypes.Contains(ItemType.Capability) || _allowedTypes.Contains(ItemType.Verb);
			tsmiNewCapability.Visible = _allowedTypes.Contains(ItemType.Verb);
			tsmiNewComponent.Visible = _allowedTypes.Contains(ItemType.Component);
			tsmiNewComposite.Visible = _allowedTypes.Contains(ItemType.Composite);
			tsmiNewDomain.Visible = _allowedTypes.Contains(ItemType.Domain);
			tsmiNewNoun.Visible = _allowedTypes.Contains(ItemType.Noun);
			tsmiNewOrchestration.Visible = _allowedTypes.Contains(ItemType.Workflow);
			tsmiNewSOA.Visible = _allowedTypes.Contains(ItemType.SOA);
			tsmiNewSOBA.Visible = _allowedTypes.Contains(ItemType.SOBA);
			tsmiNewWebService.Visible = _allowedTypes.Contains(ItemType.WebService);
			tsmiNewRuleConstant.Visible = _allowedTypes.Contains(ItemType.RuleConstant);
			tsmiNewHumanActivity.Visible = _allowedTypes.Contains(ItemType.HumanActivity);
			tsmiNewLogicalMethodGroup.Visible = _allowedTypes.Contains(ItemType.LogicalMethodGroup);
			tsmiNewLogicalMethod.Visible = _allowedTypes.Contains(ItemType.LogicalWebMethod);
		}
		private void loadRepositoryHierarchy()
		{
			_dtHierarchy = Global.Data.GetRepositoryHierarchy(this.RepositoryID);
			//if ( _dtHierarchy != null )
			//    addDomainIDToRows(_dtHierarchy);
		}

		private void fillDomainList()
		{
			if ( _dtHierarchy == null ) loadRepositoryHierarchy();

			tscmbDomain.BeginUpdate();
			tscmbFolder.BeginUpdate();
			try {
				tscmbDomain.Items.Clear();
				tscmbFolder.Items.Clear();
				if ( this.ShowAllDomains ) {
					// add "All Domains" item
					tscmbDomain.Items.Add(new DomainItem(null, 0, "All Domains", null, null));
				}
				// select top domains
				DataRow[] rows;
				rows = _dtHierarchy.Select("parent_reptype_id IS NULL AND parent_id IS NULL AND " +
							 string.Format("child_reptype_id = {0}", (int)ItemType.Domain));

				foreach ( DataRow row in rows ) {
					// add domain to combobox
					addDomainToList(row, 0);
				}

				// auto-width combobox dropdown
				using ( Graphics g = tscmbDomain.ComboBox.CreateGraphics() ) {
					foreach ( DomainItem dom in tscmbDomain.Items ) {
						SizeF sz = g.MeasureString(dom.Text, tscmbDomain.Font);
						int itemwidth = (int)Math.Ceiling(sz.Width) + dom.Indent + _textGap + 18/*scrollbar*/;
						if ( itemwidth > tscmbDomain.DropDownWidth )
							tscmbDomain.DropDownWidth = itemwidth;
					}
				}
			} finally {
				tscmbDomain.EndUpdate();
				tscmbFolder.EndUpdate();
			}
		}
		private void addDomainToList(DataRow currentRow, int indent)
		{
			ItemType type = (ItemType)currentRow["child_reptype_id"];
			int id = (int)currentRow["child_id"];

			if ( !this.AllowChangeDomain && id != this.DomainID )
				return;

			IRepositoryItem item = this.RepositoryService.GetItem(type, id, false);

			string text = currentRow["child_display"].ToString();
			string imgkey = currentRow.IsNull("child_image_key") ? item.ImageKey : currentRow["child_image_key"].ToString();

			// create domain item if current item is a domain
			DomainItem currentDomainItem = new DomainItem(imageList.Images[imgkey], indent, text, item, currentRow);
			tscmbDomain.Items.Add(currentDomainItem);

			// get child domains
			DataRow[] rows = _dtHierarchy.Select(string.Format(
					"parent_reptype_id = {0} AND parent_id = {1} AND child_reptype_id = {2}",
					(int)type, id, (int)ItemType.Domain));

			foreach ( DataRow row in rows ) {
				addDomainToList(row, indent + 1);
			}
		}
		private void addFoldersToDomain(DomainItem domain, DataRow currentRow, int indent,
										FolderItem parentFolder)
		{
			if ( indent > 5 ) return;	// avoid endless loops

			ItemType type = ItemType.Undefined;
			int id = -1;
			FolderItem currentFolder = null;
			if ( currentRow != null ) {
				// add current row to folder list
				type = (ItemType)currentRow["child_reptype_id"];
				id = (int)currentRow["child_id"];
				if ( type == ItemType.GenericFolder ) {
					GenericFolder folder = this.RepositoryService.GetItem(type, id, false) as GenericFolder;
					if ( !this.AllowChangeType ) {
						List<ItemType> ftypes = folder.AllowedChildTypes;
						bool contained = false;
						foreach ( ItemType t in this._allowedTypes ) {
							if ( t == ItemType.BRule ) {
								if ( ftypes.Contains(ItemType.BPolicy) ) {
									if ( _allowedTypes.Contains(ItemType.Route) ) {
										contained = ftypes.Contains(ItemType.Route);
									} else if ( _allowedTypes.Contains(ItemType.LogicalWebMethod) ) {
										contained = ftypes.Contains(ItemType.LogicalWebMethod);
									} else {
										contained = !ftypes.Contains(ItemType.Route);
									}
									break;
								}
							} else if ( t == ItemType.LogicalWebMethod ) {
								if ( ftypes.Contains(ItemType.LogicalMethodGroup) ) {
									contained = true;
									break;
								}
							} else if ( t == ItemType.Verb ) {
								if ( ftypes.Contains(ItemType.Composite) ) {
									contained = true;
									break;
								}
							} else if ( t == ItemType.Workflow_Field ) {
								if ( folder.AllowedChildTypes.Contains(ItemType.Workflow) ) {
									if ( !_bShowWorkflowSpecific ) continue;
									Repository.Workflow wf = RepositoryService.GetItem<Repository.Workflow>(_idWorkflow);
									if ( wf == null ) continue;
									BindingListEx<GenericFolder> items = Global.Data.GetRepositoryHierarchyChildRepositoryList<GenericFolder>(wf);
									foreach ( GenericFolder item in items ) {
										if ( item.AllowedChildTypes.Contains(ItemType.Noun) ) {
											currentFolder = new FolderItem(item.Image, indent, item.Name, item, item.row, null);
											domain.FolderItems.Add(currentFolder);
											return;
										}
									}
								}
								//currentFolder = new FolderItem(null, indent, "Process Flow Variables");

							} else {
								if ( ftypes.Contains(t) ) {
									contained = true;
									break;
								}
							}
						}
						if ( !contained )
							return;
					}
					string text = currentRow["child_display"].ToString();
					string imgkey = currentRow.IsNull("child_image_key") ? folder.ImageKey : currentRow["child_image_key"].ToString();
					currentFolder = new FolderItem(imageList.Images[imgkey], indent, text, folder, currentRow, parentFolder);
					domain.FolderItems.Add(currentFolder);
				} else if ( type == ItemType.BPolicy ) {
					BusinessPolicy policy = this.RepositoryService.GetItem(type, id, false) as BusinessPolicy;
					string text = currentRow["child_display"].ToString();
					string imgkey = currentRow.IsNull("child_image_key") ? policy.ImageKey : currentRow["child_image_key"].ToString();
					currentFolder = new FolderItem(imageList.Images[imgkey], indent, text, policy, currentRow, parentFolder);
					domain.FolderItems.Add(currentFolder);
				} else if ( type == ItemType.LogicalMethodGroup ) {
					LogicalMethodGroup lmgroup = this.RepositoryService.GetItem(type, id, false) as LogicalMethodGroup;
					string text = currentRow["child_display"].ToString();
					string imgkey = currentRow.IsNull("child_image_key") ? lmgroup.ImageKey : currentRow["child_image_key"].ToString();
					currentFolder = new FolderItem(imageList.Images[imgkey], indent, text, lmgroup, currentRow, parentFolder);
					domain.FolderItems.Add(currentFolder);
				} else if ( type == ItemType.Capability ) {
					Capability cap = this.RepositoryService.GetItem(type, id, false) as Capability;
					string text = currentRow["child_display"].ToString();
					string imgkey = currentRow.IsNull("child_image_key") ? cap.ImageKey : currentRow["child_image_key"].ToString();
					currentFolder = new FolderItem(imageList.Images[imgkey], indent, text, cap, currentRow, parentFolder);
					domain.FolderItems.Add(currentFolder);
				} else if ( type == ItemType.Composite ) {
					ServiceModule sm = this.RepositoryService.GetItem(type, id, false) as ServiceModule;
					string text = currentRow["child_display"].ToString();
					string imgkey = currentRow.IsNull("child_image_key") ? sm.ImageKey : currentRow["child_image_key"].ToString();
					currentFolder = new FolderItem(imageList.Images[imgkey], indent, text, sm, currentRow, parentFolder);
					domain.FolderItems.Add(currentFolder);
				} else if ( type == ItemType.Workflow ) {
				} else {
					return;
				}
			}
			// get child folders
			DataRow[] rows = null;
			if ( currentRow == null ) {
				if ( domain.Item != null ) {
					// of current domain
					rows = _dtHierarchy.Select(string.Format(
							"parent_reptype_id = {0} AND parent_id = {1} AND child_reptype_id = {2}",
							(int)ItemType.Domain, domain.Item.ID, (int)ItemType.GenericFolder),
							"child_display ASC");
				}
			} else {
				// of current folder
				if ( this._allowedTypes.Contains(ItemType.BRule) ) {
					rows = _dtHierarchy.Select(string.Format(
							"parent_reptype_id = {0} AND parent_id = {1} AND " +
							"( child_reptype_id = {2} OR child_reptype_id = {3} )",
							(int)type, id, (int)ItemType.GenericFolder, (int)ItemType.BPolicy),
							"child_display ASC");
				} else if ( this._allowedTypes.Contains(ItemType.LogicalWebMethod) ) {
					rows = _dtHierarchy.Select(string.Format(
							"parent_reptype_id = {0} AND parent_id = {1} AND " +
							"( child_reptype_id = {2} OR child_reptype_id = {3} )",
							(int)type, id, (int)ItemType.GenericFolder, (int)ItemType.LogicalMethodGroup),
							"child_display ASC");
				} else if ( this._allowedTypes.Contains(ItemType.Verb) ) {
					rows = _dtHierarchy.Select(string.Format(
							"parent_reptype_id = {0} AND parent_id = {1} AND " +
							"( child_reptype_id = {2} OR child_reptype_id = {3} )",
							(int)type, id, (int)ItemType.GenericFolder, (int)ItemType.Composite),
							"child_display ASC");
				} else {
					rows = _dtHierarchy.Select(string.Format(
							"parent_reptype_id = {0} AND parent_id = {1} AND child_reptype_id = {2}",
							(int)type, id, (int)ItemType.GenericFolder),
							"child_display ASC");
				}
			}
			if ( rows != null ) {
				foreach ( DataRow row in rows ) {
					addFoldersToDomain(domain, row, indent + 1, currentFolder);
				}
			}
		}
		private void addDomainItem(DomainItem item)
		{
			tscmbDomain.Items.Add(item);
		}

		private void fillFolderComboBox(DomainItem domainItem)
		{
			tscmbFolder.BeginUpdate();
			try {
				if ( domainItem.FolderItems.Count == 0 ) {
					//fillDomainListRecursive(domainItem.Row, 0, domainItem, false);
					addFoldersToDomain(domainItem, null, -1, null);
				}
				tscmbFolder.Items.Clear();
				if ( !this.ShowParentItemOnly ) {
					tscmbFolder.Items.AddRange(domainItem.FolderItems.ToArray());
				}
				// if exists: add parent item
				if ( this.ParentItem != null ) {
					tscmbFolder.Items.Insert(0,
						new FolderItem(imageList.Images[this.ParentItem.ImageKey], 0, this.ParentItem.Name, this.ParentItem, null, null));
				}
				// auto-width combobox dropdown
				tscmbFolder.ComboBox.DropDownWidth = tscmbFolder.ComboBox.Width;
				using ( Graphics g = tscmbFolder.ComboBox.CreateGraphics() ) {
					foreach ( FolderItem item in tscmbFolder.Items ) {
						SizeF sz = g.MeasureString(item.Text, tscmbFolder.Font);
						int itemwidth = (int)Math.Ceiling(sz.Width) + ( item.Indent * _indentWidth ) +
										_textGap + 25/*scrollbar*/;
						if ( itemwidth > tscmbFolder.DropDownWidth )
							tscmbFolder.DropDownWidth = itemwidth;
					}
				}
			} finally {
				tscmbFolder.EndUpdate();
			}
		}
		//private void addDomainIDToRows(DataTable dt)
		//{
		//    dt.Columns.Add("domain_id", typeof(int));
		//    DataRow[] rows = dt.Select(
		//        string.Format("parent_reptype_id IS NULL AND child_reptype_id = {0}", (int)ItemType.Domain));
		//    foreach ( DataRow row in rows ) {
		//        int domainid = (int)row["child_id"];
		//        row["domain_id"] = domainid;
		//        addDomainIDToRowsRecursive(dt, ItemType.Domain, domainid, domainid);
		//    }
		//}
		//private void addDomainIDToRowsRecursive(DataTable dt, ItemType currenttype, int currentid, int domainid)
		//{
		//    DataRow[] rows = dt.Select(
		//        string.Format("parent_reptype_id = {0} AND parent_id = {1}", (int)currenttype, currentid));
		//    foreach ( DataRow row in rows ) {
		//        row["domain_id"] = domainid;
		//        ItemType typechild = (ItemType)row["child_reptype_id"];
		//        if ( typechild == ItemType.Domain )
		//            addDomainIDToRowsRecursive(dt, typechild, (int)row["child_id"], (int)row["child_id"]);
		//        else
		//            addDomainIDToRowsRecursive(dt, typechild, (int)row["child_id"], domainid);
		//    }
		//}

		private void fillMainList()
		{
			DataTable dt;
			dgvItems.Rows.Clear();
			if ( !_bFilterByIOBObjects || this.ParentItem == null ) {
				DomainItem di = tscmbDomain.SelectedItem as DomainItem;
				if ( di.Item == null ) {
					// show "all domains" items
					if ( _bFilterByIOBObjects && AllowedTypes.Length > 0 &&
						 AllowedTypes[0] == ItemType.Verb ) {
						dt = Global.Data.GetVerbs(ItemType.Undefined, 0, _idInputBObject, _idOutputBObject);
					} else if ( AllowedTypes.Length == 1 ) {
						dt = Global.Data.GetRepositoryHierarchyChildRepositoryList(ItemType.Undefined, 0,
							Global.CurrentUser.OrganizationID, AllowedTypes[0]);
					} else {
						dt = Global.Data.GetRepositoryHierarchyChildRepositoryList(ItemType.Undefined, 0,
							Global.CurrentUser.OrganizationID, ItemType.Undefined);
					}
				} else {
					// show selected folder items
					FolderItem fi = tscmbFolder.SelectedItem as FolderItem;

					if ( fi == null ) {
						return;
					}
					BindingListEx<IRepositoryItem> items = Global.Data.GetRepositoryHierarchyParentsList(fi.Item);
					if ( _bShowWorkflowSpecific && items.Count == 1 && items[0] is Repository.Workflow ) {
						Repository.Workflow wf = RepositoryService.GetItem<Repository.Workflow>(_idWorkflow);
						BindingListEx<RepositoryItemBase> fields = new BindingListEx<RepositoryItemBase>();
						foreach ( WorkflowField field in wf.Interface.Fields ) {
							fields.Add((RepositoryItemBase)field);
						}
						dgvItems.RowCount = 0;
						fields.Sort("Name", ListSortDirection.Ascending);
						_dtMain = null;
						_listMain = fields;
						dgvItems.RowCount = fields.Count;
						dgvItems.AutoResizeColumns();
						return;
					} else {
						if ( _bFilterByIOBObjects && AllowedTypes.Length > 0 &&
							 AllowedTypes[0] == ItemType.Verb ) {
							dt = Global.Data.GetVerbs(
								fi.Item.RepositoryItemType, fi.Item.ID, _idInputBObject, _idOutputBObject);
						} else {
							dt = Global.Data.GetRepositoryHierarchyChildRepositoryList(fi.Item);
						}
					}
				}
			} else {
				if ( _bFilterByIOBObjects && AllowedTypes.Length > 0 &&
					 AllowedTypes[0] == ItemType.Verb ) {
					dt = Global.Data.GetVerbs(
						this.ParentItem.RepositoryItemType, this.ParentItem.ID,
						_idInputBObject, _idOutputBObject);
				} else {
					dt = Global.Data.GetRepositoryHierarchyChildRepositoryList(this.ParentItem);
				}
			}
			dgvItems.RowCount = 0;
			dt.DefaultView.Sort = "name ASC";
			_dtMain = dt.DefaultView.ToTable();
			_listMain = null;
			dgvItems.RowCount = dt.Rows.Count;
			dgvItems.AutoResizeColumns();
		}
		private void updateAll()
		{
			if ( this.ParentItem == null ) {
				loadRepositoryHierarchy();
				this.Refresh();
				fillDomainList();
				this.Refresh();
				if ( this.DomainID > 0 )
					selectDomain(this.DomainID);
				else
					tscmbDomain.SelectedIndex = 0;
			} else {
				tslblDomain.Visible = true;
				tslblFolder.Visible = true;
				tscmbDomain.Visible = true;
				tscmbFolder.Visible = true;
				loadRepositoryHierarchy();
				fillDomainList();
				if ( this.DomainID > 0 )
					selectDomain(this.DomainID);
				else
					tscmbDomain.SelectedIndex = 0;
			}
		}

		private void setFolderSearchBar(bool isFolderView)
		{
			toolStrip2.SuspendLayout();
			try {
				//show domain list in search mode too
				//tslblDomain.Visible = isFolderView /*&& this.ParentItem == null*/;
				//tscmbDomain.Visible = isFolderView /*&& this.ParentItem == null*/;
				_bSearchMode = !isFolderView;
				tslblFolder.Visible = isFolderView /*&& this.ParentItem == null*/;
				tscmbFolder.Visible = isFolderView /*&& this.ParentItem == null*/;
				tssepSearch.Visible = isFolderView /*&& this.ParentItem == null*/;
				tsbtnSearch.Visible = isFolderView /*&& this.ParentItem == null*/;

				tslblSearchFor.Visible = !isFolderView;
				tstxtSearchText.Visible = !isFolderView;
				tslblType.Visible = !isFolderView;
				tscmbSearchColumn.Visible = !isFolderView;
				tsbtnSearchNow.Visible = !isFolderView;
				tssepFolders.Visible = !isFolderView;
				tsbtnFolders.Visible = !isFolderView;
			} finally {
				toolStrip2.ResumeLayout(true);
			}
		}

		private void selectDomain(int id)
		{
			DomainItem diSelect = null;
			foreach ( DomainItem di in tscmbDomain.Items ) {
				if ( di.Item == null )
					continue;
				if ( di.Item.ID == id ) {
					diSelect = di;
					break;
				}
			}
			if ( diSelect != null )
				tscmbDomain.SelectedItem = diSelect;
		}
		private void selectFolder(int id)
		{
			FolderItem fiSelect = null;
			foreach ( FolderItem fi in tscmbFolder.Items ) {
				if ( fi.Item.ID == id ) {
					fiSelect = fi;
					break;
				}
			}
			if ( fiSelect != null )
				tscmbFolder.SelectedItem = fiSelect;
		}

		private void searchNow()
		{
			int domainID = SelectedDomain == null ? 0 : SelectedDomain.ID;
			string name = null;
			string desc = null;
			if ( tscmbSearchColumn.Text == _fields.AllFields ) {
				name = tstxtSearchText.Text; ;
				desc = name;
			} else if ( tscmbSearchColumn.Text == _fields.Name ) {
				name = tstxtSearchText.Text;
			} else if ( tscmbSearchColumn.Text == _fields.Description ) {
				desc = tstxtSearchText.Text;
			}
			DataTable dt = new DataTable();
			foreach ( ItemType type in _allowedTypes ) {
				if ( name != null ) {
					dt = mergeDataTabelDistinct(
						Global.Data.SearchRepositoryItems(domainID, type, name, null, false),
						dt);
				}
				if ( desc != null ) {
					dt = mergeDataTabelDistinct(
						Global.Data.SearchRepositoryItems(domainID, type, null, desc, false),
						dt);
				}
			}
			this.dgvItems.RowCount = 0;
			_dtMain = dt;
			this.dgvItems.RowCount = dt.Rows.Count;
			this.dgvItems.AutoResizeColumns();
		}

		private DataTable mergeDataTabelDistinct(DataTable source, DataTable target)
		{
			if ( target.Rows.Count != 0 && target.Columns.Count > 1 ) {
				target.PrimaryKey = new DataColumn[] { target.Columns[0] };
			}
			foreach ( DataRow row in source.Rows ) {
				if ( target.Rows.Count == 0 ) {
					target.Merge(source);
					break;
				} else {
					if ( target.Columns.Count > 1 ) {
						if ( target.Rows.Contains(row[0]) ) {
							target.LoadDataRow(row.ItemArray, LoadOption.OverwriteChanges);
						}
					} else {
						target.ImportRow(row);
					}
				}
			}
			return target;
		}

		#endregion

		#region Event Handlers

		private void FormSelectRepositoryEx_Load(object sender, EventArgs e)
		{
			this.Show();
			this.Refresh();
			this.SuspendLayout();
			try {
				updateAll();
				setFolderSearchBar(true);
			} finally {
				this.ResumeLayout(true);
			}
		}

		private void ComboBox_DrawItem(object sender, DrawItemEventArgs e)
		{
			ComboBox cmb = sender as ComboBox;
			if ( cmb == null )
				return;

			e.DrawBackground();
			if ( ( e.State & DrawItemState.Focus ) == DrawItemState.Focus )
				e.DrawFocusRectangle();

			if ( e.Index < 0 || e.Index >= cmb.Items.Count ) {
				using ( StringFormat sf = new StringFormat() ) {
					sf.Trimming = StringTrimming.None;
					sf.Alignment = StringAlignment.Center;
					sf.LineAlignment = StringAlignment.Center;
					e.Graphics.DrawString("- No Folder Available -", e.Font, SystemBrushes.GrayText, e.Bounds, sf);
				}
			} else {
				FolderItem item = cmb.Items[e.Index] as FolderItem;
				if ( item != null ) {
					int indent = _indentWidth * item.Indent + 2;
					int textGap = 0;

					// draw image
					if ( item.Image != null ) {
						textGap = _textGap;
						Rectangle rectImg = new Rectangle(e.Bounds.X + indent, e.Bounds.Y,
														  _textGap, e.Bounds.Height);
						Bitmap bmp = item.Image as Bitmap;
						bmp.MakeTransparent(bmp.GetPixel(1, 1));
						e.Graphics.DrawImageUnscaled(bmp, rectImg);
					}
					// draw text
					RectangleF rectText = new RectangleF(e.Bounds.Left + textGap + indent, e.Bounds.Y,
														 e.Bounds.Width - textGap - indent, e.Bounds.Height);
					using ( StringFormat sf = new StringFormat() )
					using ( Brush b = new SolidBrush(e.ForeColor) ) {
						sf.Trimming = StringTrimming.EllipsisCharacter;
						if ( item.Item == null )	// All <ITEMTYPENAME>
							sf.Alignment = StringAlignment.Center;
						else
							sf.Alignment = StringAlignment.Near;
						sf.LineAlignment = StringAlignment.Center;
						e.Graphics.DrawString(item.Text, e.Font, b, rectText, sf);
					}

				} else {
					using ( StringFormat sf = new StringFormat() )
					using ( Brush b = new SolidBrush(e.ForeColor) ) {
						sf.Trimming = StringTrimming.EllipsisCharacter;
						sf.Alignment = StringAlignment.Near;
						sf.LineAlignment = StringAlignment.Center;
						e.Graphics.DrawString(cmb.Items[e.Index].ToString(), e.Font, b, e.Bounds, sf);
					}
				}
			}
		}
		private void dgvItems_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
		{
			RepositoryService repSvc = (RepositoryService)this.GetService(typeof(RepositoryService));
			if ( repSvc == null )
				throw new NullReferenceException("RepositoryService not available.");

			if ( e.RowIndex >= 0 && dgvItems.Columns[e.ColumnIndex].Name == colType.Name && e.Value is int &&
				 Enum.IsDefined(typeof(ItemType), (int)e.Value) ) {
				// custom paint at itemtype column
				ItemType type = (ItemType)e.Value;
				bool selected = ( ( e.State & DataGridViewElementStates.Selected ) > 0 );
				e.PaintBackground(e.CellBounds, selected);
				string key = "";
				if ( selected )
					key = repSvc.GetSelectedImageKey(type);
				else
					key = repSvc.GetImageKey(type);
				if ( key != "" ) {
					Rectangle r = e.CellBounds;
					r.Inflate(-1, -1);
					Image img = imageList.Images[key];
					if ( img != null )
						e.Graphics.DrawImageUnscaled(img, r.X, r.Y, 18, 18);
				}
				e.Handled = true;
			} else if ( e.RowIndex > -1 && e.ColumnIndex == colSelect.Index ) {
				DataGridViewRow row = dgvItems.Rows[e.RowIndex];
				if ( _dtMain != null ) {
					if ( row != null ) {
						RepositoryItemKey key = new RepositoryItemKey((ItemType)_dtMain.Rows[e.RowIndex]["type"],
																	  (int)_dtMain.Rows[e.RowIndex]["id"]);
						if ( !_allowedTypes.Contains(key.Type) ) {
							bool selected = ( ( e.State & DataGridViewElementStates.Selected ) > 0 );
							e.PaintBackground(e.CellBounds, selected);
							e.Handled = true;
						}
					}
				} else if ( _listMain != null ) {
					RepositoryItemKey key = new RepositoryItemKey(_listMain[e.RowIndex].RepositoryItemType,
																  _listMain[e.RowIndex].ID);
					if ( !_allowedTypes.Contains(key.Type) ) {
						bool selected = ( ( e.State & DataGridViewElementStates.Selected ) > 0 );
						e.PaintBackground(e.CellBounds, selected);
						e.Handled = true;
					}
				}
			} else if ( e.RowIndex >= 0 && dgvItems.Columns[e.ColumnIndex].Name == colType.Name ) {
			} else {
				e.Handled = false;
			}
		}

		private void onTypeSelected(object sender, EventArgs e)
		{

		}

		private void tscmbDomain_SelectedIndexChanged(object sender, EventArgs e)
		{
			if ( !_bSearchMode ) {
				DomainItem domain = tscmbDomain.SelectedItem as DomainItem;
				if ( domain != null ) {
					fillFolderComboBox(domain);
					if ( tscmbFolder.Items.Count > 0 )
						tscmbFolder.SelectedIndex = 0;
					else if ( domain.Item == null )
						fillMainList();
					else
						dgvItems.Rows.Clear();
				}
			}
		}
		private void tscmbFolder_SelectedIndexChanged(object sender, EventArgs e)
		{
			fillMainList();
		}

		private void dgvItems_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if ( /*e.ColumnIndex == colSelect.Index &&*/ e.RowIndex > -1 ) {
				DataGridViewRow row = dgvItems.Rows[e.RowIndex];
				DataGridViewCheckBoxCell cell = row.Cells[colSelect.Index] as DataGridViewCheckBoxCell;
				if ( cell != null ) {
					RepositoryItemKey key = null;
					if ( _listMain != null ) {
						key = new RepositoryItemKey(_listMain[e.RowIndex].RepositoryItemType,
													_listMain[e.RowIndex].ID);
					} else {
						key = new RepositoryItemKey((ItemType)_dtMain.Rows[e.RowIndex]["type"],
																  (int)_dtMain.Rows[e.RowIndex]["id"]);
					}
					if ( !_allowedTypes.Contains(key.Type) )
						return;

					if ( _selectedItems.Contains(key) ) {
						_selectedItems.Remove(key);
						cell.Value = false;
						if ( _lastCheckedCell == cell ) {
							_lastCheckedCell = null;
						}
						dgvItems.UpdateCellValue(cell.ColumnIndex, cell.RowIndex);
					} else {
						cell.Value = true;
						if ( !this.MultiSelect ) {
							_selectedItems.Clear();
							if ( _lastCheckedCell != null && !this.MultiSelect ) {
								_lastCheckedCell.Value = false;
								if ( _lastCheckedCell.Displayed )
									dgvItems.UpdateCellValue(_lastCheckedCell.ColumnIndex, _lastCheckedCell.RowIndex);
							}
							_lastCheckedCell = cell;
						}
						_selectedItems.Add(key);
					}
				}
			}
		}
		private void dgvItems_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			if ( e.RowIndex > -1 ) {
				DataGridViewRow row = dgvItems.Rows[e.RowIndex];
				if ( row != null ) {
					RepositoryItemKey key = new RepositoryItemKey((ItemType)_dtMain.Rows[e.RowIndex]["type"],
																  (int)_dtMain.Rows[e.RowIndex]["id"]);
					if ( key.Type == ItemType.GenericFolder || key.Type == ItemType.BPolicy ||
						 key.Type == ItemType.Capability || key.Type == ItemType.Composite ) {
						foreach ( FolderItem fi in tscmbFolder.Items ) {
							if ( fi.Item.RepositoryItemType == key.Type && fi.Item.ID == key.ID ) {
								tscmbFolder.SelectedItem = fi;
								break;
							}
						}
					}
				}
			}
		}

		private void dgvItems_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
		{
			if ( e.RowIndex < 0 ||
				( _dtMain != null && e.RowIndex >= _dtMain.Rows.Count ) ||
				( _listMain != null && e.RowIndex >= _listMain.Count ) )
				return;

			if ( _dtMain == null ) {
				if ( e.ColumnIndex == colSelect.Index ) {
					RepositoryItemKey key = new RepositoryItemKey(_listMain[e.RowIndex].RepositoryItemType,
																  _listMain[e.RowIndex].ID);
					bool isSelected = _selectedItems.Contains(key);
					e.Value = isSelected;
					if ( isSelected ) {
						_lastCheckedCell = (DataGridViewCheckBoxCell)dgvItems[colSelect.Index, e.RowIndex];
					}
				} else {
					switch ( dgvItems.Columns[e.ColumnIndex].DataPropertyName ) {
						case "type":
							e.Value = (int)_listMain[e.RowIndex].RepositoryItemType;
							break;
						case "name":
							if ( _listMain[e.RowIndex] is WorkflowField ) {
								WorkflowField field = _listMain[e.RowIndex] as WorkflowField;
								e.Value = string.Format("{0} {{{1}}}", field.Name, field.DataType);
							} else {
								e.Value = _listMain[e.RowIndex].Name;
							}
							break;
						case "desc":
							e.Value = _listMain[e.RowIndex].Description;
							break;
					}
				}
				//e.Value = null;
			} else {
				if ( e.ColumnIndex == colSelect.Index ) {
					RepositoryItemKey key = new RepositoryItemKey((ItemType)_dtMain.Rows[e.RowIndex]["type"],
																  (int)_dtMain.Rows[e.RowIndex]["id"]);
					bool isSelected = _selectedItems.Contains(key);
					e.Value = isSelected;
					if ( isSelected ) {
						_lastCheckedCell = (DataGridViewCheckBoxCell)dgvItems[colSelect.Index, e.RowIndex];
					}
				} else {
					string datacolnm = dgvItems.Columns[e.ColumnIndex].DataPropertyName;
					e.Value = _dtMain.Rows[e.RowIndex][datacolnm];
				}
			}
		}
		private void dgvItems_CellValuePushed(object sender, DataGridViewCellValueEventArgs e)
		{
			if ( e.RowIndex < 0 )
				return;

			if ( _dtMain != null ) {
				if ( e.ColumnIndex == 0 ) {
					//RepositoryItemKey key = new RepositoryItemKey((ItemType)_dtMain.Rows[e.RowIndex]["type"],
					//                                              (int)_dtMain.Rows[e.RowIndex]["id"]);
					//if ( e.Value is bool && (bool)e.Value == true ) {
					//    if ( !_selectedItems.Contains(key) )
					//        _selectedItems.Add(key);
					//} else if ( _selectedItems.Contains(key) ) {
					//    _selectedItems.Remove(key);
					//}
				} else {
					string datacolnm = dgvItems.Columns[e.ColumnIndex].DataPropertyName;
					_dtMain.Rows[e.RowIndex][datacolnm] = e.Value;
				}
			} else {
			}
		}

		private void tsbtnRefresh_Click(object sender, EventArgs e)
		{
			if ( this.ParentItem == null ) {
				int selDom = tscmbDomain.SelectedIndex;
				int selFol = tscmbFolder.SelectedIndex;
				tscmbDomain.BeginUpdate();
				tscmbFolder.BeginUpdate();
				try {
					fillDomainList();
				} finally {
					tscmbDomain.EndUpdate();
					tscmbFolder.EndUpdate();
				}
				if ( tscmbDomain.Items.Count > selDom )
					tscmbDomain.SelectedIndex = selDom;
				if ( tscmbFolder.Items.Count > selFol )
					tscmbFolder.SelectedIndex = selFol;
			} else {
				fillMainList();
			}
		}

		private void tsbtnOK_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.OK;
			this.Close();
		}

		private void tsbtnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		private void tsbtnCreateNew_Click(object sender, EventArgs e)
		{
		}
		private void onCreateNewItem(object sender, EventArgs e)
		{
			ItemType type = ItemType.Undefined;
			if ( sender == tsmiNewBObject )
				type = ItemType.BObject;
			else if ( sender == tsmiNewBProcess )
				type = ItemType.Business_Process;
			else if ( sender == tsmiNewBRule )
				type = ItemType.BRule;
			else if ( sender == tsmiNewCapability )
				type = ItemType.Verb;
			else if ( sender == tsmiNewComponent )
				type = ItemType.Component;
			else if ( sender == tsmiNewComposite )
				type = ItemType.Composite;
			else if ( sender == tsmiNewDomain )
				type = ItemType.Domain;
			else if ( sender == tsmiNewNoun )
				type = ItemType.Noun;
			else if ( sender == tsmiNewOrchestration )
				type = ItemType.Workflow;
			else if ( sender == tsmiNewSOA )
				type = ItemType.SOA;
			else if ( sender == tsmiNewSOBA )
				type = ItemType.SOBA;
			else if ( sender == tsmiNewWebService )
				type = ItemType.WebService;
			else if ( sender == tsmiNewRuleConstant )
				type = ItemType.RuleConstant;
			else if ( sender == tsmiNewService )
				type = ItemType.Composite;
			else if ( sender == tsmiNewLogicalMethodGroup )
				type = ItemType.LogicalMethodGroup;
			else if ( sender == tsmiNewLogicalMethod )
				type = ItemType.LogicalWebMethod;
			else if ( sender == tsmiNewHumanActivity )
				type = ItemType.HumanActivity;

			if ( type != ItemType.Undefined ) {
				IRepositoryItem parentItem = null;

				if ( this.SelectedFolderItem != null && this.SelectedFolderItem.AllowedChildTypes.Contains(type) ) {
					parentItem = this.SelectedFolderItem;
					//} else if ( this.SelectedDomain != null ) {
					//    parentItem = this.SelectedDomain;
				}
				Repository.IRepositoryItem item = this.RepositoryService.GetNewItem(type, true, ref parentItem);
				this.SelectedItem = item;
				updateAll();
			}
		}

		private void tsbtnSearch_Click(object sender, EventArgs e)
		{
			setFolderSearchBar(false);
			this.dgvItems.DataSource = null;
			this.dgvItems.Rows.Clear();
		}

		private void tsbtnSearchNow_Click(object sender, EventArgs e)
		{
			searchNow();
		}
		private void tsbtnFolders_Click(object sender, EventArgs e)
		{
			setFolderSearchBar(true);
			this.dgvItems.DataSource = null;
			updateAll();
		}

		private void tsbtnFolderUp_Click(object sender, EventArgs e)
		{
			FolderItem fi = tscmbFolder.SelectedItem as FolderItem;
			if ( fi != null && fi.ParentFolder != null ) {
				tscmbFolder.SelectedItem = fi.ParentFolder;
			}
		}

		private void tscmbSearchText_KeyUp(object sender, KeyEventArgs e)
		{
			if ( e.KeyCode == Keys.Enter ) {
				searchNow();
			}
		}

		private void dgvItems_KeyUp(object sender, KeyEventArgs e)
		{
			if ( e.Alt && e.Control && e.KeyCode == Keys.I ) {
				Repository.RepositoryItemBase item = dgvItems.SelectedRows[0].DataBoundItem as Repository.RepositoryItemBase;
				Helpers.OpenDebugViewer(item, this);
			}
		}
		private void dgvItems_MouseClick(object sender, MouseEventArgs e)
		{
			if ( e.Button == MouseButtons.XButton1 ) {
				FolderItem fi = tscmbFolder.SelectedItem as FolderItem;
				if ( fi != null && fi.ParentFolder != null ) {
					tscmbFolder.SelectedItem = fi.ParentFolder;
				}
			}
		}


		#endregion

		#endregion

		#region Private Static Class: _fields
		private static class _fields
		{
			public const string AllFields = "All Fields";
			public const string Name = "Name";
			public const string Description = "Description";

			public static string[] GetItems()
			{
				return new string[] { AllFields, Name, Description };
			}
		}
		#endregion
	}

	#region Internal Class: FolderItem
	internal class FolderItem
	{
		public FolderItem(Image image, int indent, string text, IRepositoryItem item, DataRow row,
						  FolderItem parentFolder)
		{
			this.Image = image;
			this.Indent = indent;
			this.Text = text;
			this.Item = item;
			this.Row = row;
			this.ParentFolder = parentFolder;
		}

		public Image Image;
		public int Indent;
		public string Text;
		public IRepositoryItem Item;
		public DataRow Row;
		public FolderItem ParentFolder;
	}
	#endregion
	#region Internal Class: DomainItem , Base Class: FolderItem
	internal class DomainItem : FolderItem
	{
		public DomainItem(Image image, int indent, string text, IRepositoryItem item, DataRow row)
			: base(image, indent, text, item, row, null)
		{
			this.FolderItems = new BindingListEx<FolderItem>();
		}

		public BindingListEx<FolderItem> FolderItems;
	}
	#endregion
}